import urls from '/util/urls'
import request from '/util/request'
const app = getApp()

Page({
  data: {
    sId: '',
    // ID
    id: '',
    // 编码
    code: '',
    // 开始时间
    startTime: '',
    // 结束时间
    endTime: '',
    //状态
    status: '',
    //单位编码
    unitCode: '',
    //单位名称
    unitName: '',
    //  执行人
    userName: '',
    //总结
    record: '',
    //附件列表
    files: [],
  },
  onLoad(options) {
    let that = this
    // 传递的参数
    const id = options.id
    // 加载巡检的详细信息
    request.get(urls.inspectionServiceSummary(id), function (data) {
      let insti = app.globalData.instituteInfo.institutions.filter(t => {
        return t.code == data.unitCode
      })
      let institutionName = data.unitCode
      if (insti.length > 0) {
        institutionName = insti[0].name
      }
      that.setData({
        sId: id,
        // ID
        id: id,
        // 编码
        code: data.code,
        // 开始时间
        startTime: data.startTime,
        // 结束时间
        endTime: data.endTime,
        // 单位编码
        unitCode: data.unitCode,
        unitName: institutionName,
        // 状态
        status: data.status,
        userName: app.globalData.userInfo.name,
        record: data.record,
        files: data.files
      })
    })
  },
  // 输入框发生变化
  onItemInput(e) {
    this.setData({
      [e.target.dataset.field]: e.detail.value,
    });
  },
  // 清除输入框
  onClear(e) {
    this.setData({
      [e.target.dataset.field]: '',
    });
  },
  deleteImg(e) {
    const that = this
    my.showActionSheet({
      title: '是否删除',
      items: ['删除'],
      cancelButtonText: '取消',
      success: (res) => {
        if (res.index != -1) {
          const fsId = e.target.dataset.field
          const one = that.data.files.find(t => t.fsId == fsId)
          const files = that.data.files.filter(t => t.fsId != fsId)
          if (one) {
            request.delete(urls.inspectionServiceDeleteFileUrl(one.fsId, that.data.id), {}, function (data) {
              that.setData({ files: files })
            }, function (err) { })
          }
        }
      },
      error(e) { }
    });
  },
  onUpLoadImageFromCamera() {
    const that = this;
    dd.chooseImage({ /** 调取相册*/
      // sourceType: ['camera', 'album'],
      sourceType: ['camera', 'album'],
      count: 9 - that.data.files.length,
      success: (res) => {
        var filePaths = res.filePaths;
        if (filePaths) {
          filePaths.forEach(path => {
            dd.uploadFile({
              url: urls.inspectionServiceUploadFileUrl(),
              fileType: 'image',
              fileName: 'file',
              filePath: path,
              headers: {
                Authentication: app.globalData.token,
                'Content-Type': 'application/json;charset=UTF-8'
              },
              success: (response) => {
                const file = JSON.parse(response.data)
                const obj = {
                  fsId: file.data.fsId,
                  name: file.data.name,
                  description: path
                }

                if (that.data.files) {
                  const fsIdArr = that.data.files.map(t => t.fsId)
                  if (!fsIdArr.includes(obj.fsId)) {
                    that.setData({
                      ['files[' + that.data.files.length + ']']: obj
                    })

                  }
                }

              },
            });
          })
        }
      },
      fail: (e) => {
        console.log('fail----------------------')
      }
    })
  },

  onSubmit() {
    const that = this
    dd.showLoading({
      content: '提交中...',
    })
    if (that.data.files.length > 0) {
      that.data.files = that.data.files.map(e => { return { fsId: e.fsId, name: e.name } })
    }

    request.put(urls.inspectionServiceSummary(that.data.id), that.data, () => {
      dd.hideLoading()
      dd.showToast({
        type: 'success',
        content: '提交成功',
        duration: 2000,
        success: () => {
          dd.redirectTo({
            url: '/pages/inspection/table/table?type=insurance'
          })
        },
      })
    }, (error) => {
      dd.hideLoading()
      dd.showToast({
        type: 'fail',
        content: error,
        duration: 2000,
      })
    })
  },
  back() {
    const that = this
    dd.redirectTo({
      url: '/pages/inspection/insurance/insurance?id=' + that.data.sId
    })
  }
});